home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / misc / sci / RARS_Amiga_2.lha / RARS / bill.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-31  |  3.3 KB  |  116 lines

  1. // BILL.CPP - "driver" function for RARS 
  2. // By Bill Benedict, Jan. 1995 
  3. // Modified from the driver 'Kernign' since it was the fastest :)
  4. // This code may or may not be easy to read.. it is still similar to
  5. // Mitchell Timin's code.
  6. // (see CNTRL0.CPP for better comments)
  7. // adapted to ver. 0.39 3/6/95 by M. Timin
  8.  
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include "car.h"
  13.  
  14. extern double width;
  15. extern const double CARWID;
  16. extern char* glob_name;
  17.  
  18. con_vec bill(situation s)
  19. {
  20.    const char name[] = "Bill";
  21.    static int init_flag = 1;
  22.    con_vec result;
  23.    double alpha, vc, rad;
  24.    double steer_gain = .2;
  25.    const double steer_damp = .48;
  26.    const double normalane = 70.0;
  27.    static int lane_time = 0;
  28.    static int lane_change = -10;
  29.  
  30.    static double lane = 70;    // determines right-left position on straigtaway
  31.    const double corn_con = 7.0;
  32.    static double corn_spd = 70.0;
  33.  
  34.    if(init_flag)  {
  35.       strcpy(glob_name, name);
  36.       init_flag = 0;
  37.       result.alpha = result.vc = 0;
  38.       return result;
  39.    }
  40.  
  41.   if(stuck(s.backward, s.v,s.vn, s.to_lft,s.to_rgt, &result.alpha,&result.vc))
  42.       return result;
  43.  
  44.    if(s.cur_rad == 0.0 && s.nex_rad != 0.0)
  45.       corn_spd = corn_con * sqrt(s.nex_rad > 0.0 ? s.nex_rad : -s.nex_rad);
  46.    else
  47.       corn_spd = corn_con * sqrt(s.cur_rad > 0.0 ? s.cur_rad : -s.cur_rad);
  48.  
  49.    // maybe choose a different lane: (to help in passing)
  50.    if(!s.dead_ahead) {
  51.       if (lane_time <= 0) {
  52.     lane_time=-1;
  53.     lane = normalane;
  54.       }
  55.    }
  56.    else if (lane_time <= 0){
  57.       // pick a different lane:
  58.       if(lane >= 80)               // pick a new lane somehow:
  59.      lane_change = -10;
  60.       else if(lane <= -80)
  61.      lane_change = 10;
  62.       lane += lane_change;
  63.       lane_time=100;
  64.  
  65.    }
  66.    if (lane_time >=0 ){
  67.      lane_time--;
  68.  
  69.    }
  70. //Code below will buzz when someone is ahead of the driver..
  71. // it is only here to facilitate testing of the dead_ahead stuff
  72. // and is borland specific.  Use at your own discretion.
  73. //if(s.dead_ahead) {sound(20);delay(2);nosound();}
  74.  
  75.    if((s.cur_rad == 0) && ((s.to_end/s.cur_len > .2) || (s.v < corn_spd))) {
  76.       alpha = .25 * steer_gain * (s.to_lft - s.to_rgt - lane) / width;
  77.       if(s.dead_ahead)
  78.       alpha *= 4.0;
  79.     }
  80.    else {
  81.       if(s.cur_rad == 0) rad=s.nex_rad;
  82.       else rad=s.cur_rad;
  83.  
  84.       if(rad > 0.0)   {
  85.     alpha = steer_gain * (3 * (s.to_lft -
  86.              (s.nex_rad < 0.0 ? width/3 : 1.6*CARWID)
  87.                     ) / width + .05 * width/s.to_rgt);
  88.       if(s.dead_ahead)
  89.      alpha *= .7;
  90.  
  91.       }
  92.       else  {
  93.     alpha = -steer_gain * (3 * (s.to_rgt -
  94.                    (s.nex_rad > 0.0 ? width/3 : 1.6*CARWID)
  95.                          ) / width  + .05 * width/s.to_lft);
  96.       if(s.dead_ahead)
  97.      alpha *= .7;
  98.       }
  99.    }
  100.    alpha -= steer_damp * s.vn / s.v;  // This is damping, to prevent oscillation
  101.  
  102.    if(s.cur_rad == 0)         // If we are on a straightaway,
  103.       if(s.to_end/s.cur_len > .14)      // if we are far from the end:
  104.      vc = s.v + 85/s.v;        // keep accellerating near full power
  105.       else  {                 // otherwise,
  106.      vc = corn_spd;           // maintain cornering speed, braking at first
  107.      // this next formula will not work if a rt. turn follows the straight:
  108.      alpha += .4 * (s.cur_len/(s.to_end + s.cur_len) - (1/1.25 ));
  109.       }
  110.    else            // if we're in the curve, maintain speed, +
  111.       vc = corn_spd + 1.2 / (s.to_end + .6);
  112.  
  113.    result.vc = vc;   result.alpha = alpha;
  114.    return result;
  115. }                                                              // v;
  116.